package com.ucode.tool.base;
import java.io.Serializable;
import java.util.List;

/**
 * 分页实体类
 * @author liliang
 * @date: 2019年12月6日 上午11:05:25
 */
public class Paginator<T> implements Serializable {

    private static final long serialVersionUID = 5918571940402089530L;
    // 总记录数
	private long total = 0L;
	// 当前页数
	private int currentPage = 1;
	// 总页数
	private long totalPage = 1;
	// 每页记录数
	private int rows = 10;
	
	private int offset = 0;
	
	private List<T> list; 

	public Paginator() {
	}
	
	public Paginator(long total, int page, int rows) {
	    setTotal(total);
	    setCurrentPage(page);
	    setRows(rows);
	}

	public Paginator(long total, int page, int rows,List<T> list) {
		setTotal(total);
		setCurrentPage(page);
		setRows(rows);
		setList(list);
	}

	public long getTotal() {
		return total;
	}

	public void setTotal(long total) {
		this.total = total;
		this.initTotalPage();
	}

	public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public long getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(long totalPage) {
		this.totalPage = totalPage;
	}

	public int getRows() {
		return rows;
	}
	
	public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public void setRows(int rows) {
		// 设置个最大记录数，限制单页记录过多
		if (rows > 10000) {
			rows = 10000;
		}
		this.rows = rows;
		this.initTotalPage();
	}

	/**
	 * 初始化分页信息
	 */
	public void initTotalPage() {
		totalPage = (total % rows) == 0 ? (total / rows) : ((total / rows) + 1);
		if (currentPage > totalPage) {
		    currentPage = (int) totalPage;
		}
		if (currentPage < 1) {
		    currentPage = 1;
		}
		offset = (currentPage-1)*rows;
	}

    public int getOffset() {
        return offset;
    }

    public void setOffset(int offset) {
        this.offset = offset;
    }

}